package common.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import util.DebugUtil;
import util.StringUtil;


public class CacheService extends BaseService implements ICacheService {
	private static Log log = LogFactory.getLog(CacheService.class);
			
	private static Map<String, List<Map<String,Object>>> cacheMap = new HashMap<String, List<Map<String, Object>>>(); 
	
	//根据缓存ID获取缓存数据
	public static List<Map<String,Object>> getDataByCacheId(String cacheId) {
		return cacheMap.get(cacheId);
	}
	
	//获取所有缓存配置数据
	public List<Map<String,Object>> getAllCacheConfig() throws Exception {
		return queryBySql("select * from " + StringUtil.getTableName("SYSMAN_CACHE_CONFIG"));
	}
	
	/**
	 * 根据条件过滤缓存数据
	 * @param cacheId 缓存ID
	 * @param columnName 列名
	 * @param columnValue 列的值
	 * @return
	 */
	public static List<Map<String,Object>> getDataByCacheId(String cacheId, String columnName, String columnValue) {
		List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> list = cacheMap.get(cacheId);
		if(list == null) {
			ICacheService service = (ICacheService) ServiceFactory.getService(new CacheService());
			try {
				list = service.refreshByCacheId(cacheId);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		for(Map<String,Object> map : list) {
			String value = String.valueOf(map.get(columnName));
			if(columnValue.equals(value)) {
				retList.add(map);
			}
		}
		return retList;
		
	}
	
	/**
	 * 根据条件过滤缓存数据
	 * @param cacheId 缓存ID
	 * @param columnName 列名
	 * @param columnValue 列的值
	 * @return
	 */
	public static Map<String,Object> getDataById(String cacheId, String columnName, String columnValue) {
		Map<String,Object> retMap = new HashMap<String,Object>();
		List<Map<String,Object>> list = cacheMap.get(cacheId);
		if(list == null) {
			ICacheService service = new CacheService();
			try {
				list = service.refreshByCacheId(cacheId);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		for(Map<String,Object> map : list) {
			String value = String.valueOf(map.get(columnName));
			if(columnValue.equals(value)) {
				return map;
			}
		}
		return retMap;
		
	}
	
	//初始化所有缓存数据
	public void initAllCacheData() throws Exception {
		List<Map<String,Object>> list = queryBySql("select * from " + StringUtil.getTableName("SYSMAN_CACHE_CONFIG"));
		int size = list.size();
		for(int i = 0;i<size;i++) {
			Map<String,Object> map = list.get(i);
			String cacheId = (String)map.get("CACHE_ID");
			String sql = (String)map.get("CACHE_SQL");
			DebugUtil.print("缓存SQL:" + sql);
			List<Map<String,Object>> cacheDataList = queryBySql(sql);
			cacheMap.put(cacheId, cacheDataList);
		}
	}
	
	//根据缓存ID刷新缓存数据
	public List<Map<String,Object>> refreshByCacheId(String cacheId) throws Exception {
		if(StringUtils.isEmpty(cacheId)) {
			initAllCacheData();
			return null;
		}
		
		Map<String,Object> map = queryById("select * from " + StringUtil.getTableName("SYSMAN_CACHE_CONFIG") + " where CACHE_ID='" + cacheId.toUpperCase() + "' ");
		String sql = (String)map.get("CACHE_SQL");
		List<Map<String,Object>> cacheDataList = queryBySql(sql);
		cacheMap.put(cacheId, cacheDataList);
		return cacheDataList;
	}
	
	//清空所有缓存数据
	public void clearAll() {
		cacheMap.clear();
		cacheMap = null;
	}
	
}
